package util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import obj.MovingPlatform;
import obj.Platform;


/**
 * Loads world data from a file.
 * @author VincentLy
 */
public class WorldLoader{

	private ArrayList<Platform> platforms;	
	private ArrayList<Rect> portals;
	private Rect savept = null;

	public WorldLoader(String s){
		platforms = new ArrayList<Platform>();
		portals = new ArrayList<Rect>();
		try {
			BufferedReader br = new BufferedReader(new FileReader(new File(s)));
			if(s!="res/battleplatforms.txt"){
				String[] temp = br.readLine().split(",");
				int[] temp2 = new int[temp.length];
				for(int i = 0;i<temp.length;i++)
					temp2[i] = Integer.parseInt(temp[i]);
				savept = new Rect(temp2[0],temp2[1],temp2[2],temp2[3],temp2[4]);
			}
			ArrayList<int[]> list = new ArrayList<int[]>();
			String line = br.readLine();	
			String[] stuff;
			int[] nums;
			while(line!=null){
				stuff = line.split(",");
				nums = new int[stuff.length];
				for(int i = 0;i<stuff.length;i++)
					nums[i] = Integer.parseInt(stuff[i]);
				list.add(nums);
				line = br.readLine();			
			}
			for(int[] n:list)
				if(n.length==5)
					portals.add(new Rect(n[0],n[1],n[2],n[3],n[4]));
				else if(n.length==6)
					platforms.add(new Platform(n[0],n[1],n[2],n[3],n[4],n[5]));
				else if(n.length==9)
					platforms.add(new MovingPlatform(n[0],n[1],n[2],n[3],n[4],n[5],n[6],n[7],n[8]));
			br.close();							    	  
		} 
		catch (IOException e) {e.printStackTrace();}				
	}

	/**
	 * @return list of platforms
	 * @author VincentLy
	 */
	public ArrayList<Platform> getPlatforms(){return platforms;}
	
	public ArrayList<Rect> getPortals(){return portals;}
	
	public Rect getSavePt(){return savept;}
}